<?php
/**
 * 图片上传相关
 *
 * Author:李强
 * Company:上海煦鸣网络科技有限公司
 * Date:2018-01-03
 */

namespace App\Http\Controllers\Component;

use App\Http\Controllers\CommonController;
use App\Models\Attachment;
use App\Models\Role;
use App\Services\Service\Helper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UploadController extends CommonController
{
    /**
     * 图片文件上传
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function upload(Request $request)
    {
        //图片上传模式1单图片2多图片
        $mode = $request->input('mode', 1);
        if (!in_array($mode, [1, 2])) $mode = 1;
        $this->assign('mode', $mode);
        //图片最多上传个数0为不限制
        $limit = $request->input('number', 0);
        $this->assign('number', $limit);
        //图片大小限制,默认4M
        $size = $request->input('size', '4096');
        $this->assign('size', $size);
        //设定文件域的字段名
        $field = $request->input('field', 'file');
        $this->assign('field', $field);
        //打开弹窗的业务端home前台admin后台管理
        $client = $request->input('client', 'home');
        $this->assign('client', $client);
        return $this->display('component.upload');
    }

    /**
     *  上传文件
     *
     * @param Request $request
     * @param $client
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploader(Request $request, $client)
    {
        if (!in_array($client, ['admin', 'home'])) {
            return $this->error('上传类型错误');
        }
        if ($client == 'admin') {
            if (!Auth::check()) {
                return $this->error('需要登陆才能上传');
            }
            $type = 1;
            $user_id = Auth::id();
        } elseif ($client == 'home') {
            if (!Auth::check()) {
                return $this->error('需要登陆才能上传');
            }
            $type = 2;
            $user_id = Auth::id();
        }

        $name = $request->input('name', 'file');
        $file = $request->file($name)->storePublicly(date('ym'));
        if ($file) {
            $path = '/storage/' . $file;
            $names = explode('.', $request->file($name)->getClientOriginalName());

            $result = Helper::uploadImageToRemote(public_path($path), date('Ym'),$path);
            if ($result['code'] == 1) {
                $data = [
                    'name' => $names['0'],
                    'file_name' => $request->file($name)->getClientOriginalName(),
                    'path' => $result['url'],
                    'extension' => $request->file($name)->getClientOriginalExtension(),
                    'size' => $request->file($name)->getSize(),
                    'status' => '1',
                    'type' => $type,
                    'user_id' => $user_id
                ];
                $attachment = Attachment::create($data);
                if (false !== $attachment) {
                    return $this->success('成功', ['data' => ['id' => $attachment->id, 'path' => $result['url'], 'name' => $attachment->name]]);
                } else {
                    return $this->error('上传失败');
                }
            }else{
                return $this->error('上传失败'.$result['message']);
            }
        } else {
            return $this->error('上传失败');
        }
    }

    /**
     * 文件列表
     *
     * @param Request $request
     * @return array
     */
    public function fileLists(Request $request, $client)
    {
        if ($request->ajax()) {
            $options = [
                'extension' => explode(',', strtolower($request->extensions)),
            ];
            if (!in_array($client, ['admin', 'home'])) {
                return $this->error('上传类型错误');
            }

            $where = [];
            if ($client == 'admin') {
                if (!Auth::check()) {
                    return $this->error('需要登陆才能上传');
                }
                if (!Role::isAdmin()) {
                    $where['member_id'] = Auth::id();
                }
                $where['type'] = 1;
            } elseif ($client == 'home') {
                if (!Auth::check()) {
                    return $this->error('需要登陆才能上传');
                }
                $where['type'] = 2;
                $where['member_id'] = Auth::id();
            }
            $start = $request->input('start', 0);
            $size = $request->input('size', 20);
            $query = Attachment::where($where)->offset($start)->limit($size);
            $total = $query->count();
            $datas = $query->get();
            $data = [];
            if (!empty($datas)) {
                foreach ($datas as $key => $value) {
                    $data[$key]['url'] = $value['path'];
                    $data[$key]['aid'] = $value['id'];
                    $data[$key]['title'] = $value['name'];
                }
            }
            return $this->success('success', ['list' => $data, 'total' => $total, 'start' => $start]);
        }
    }
}
